import Vue from 'vue'
import Vuex from 'vuex'
import {
  setToken,
  getToken
} from '@/lib/util'
Vue.use(Vuex)
import iView from 'view-design'
import {
  login
} from '@/api/api.js'


export default new Vuex.Store({
  state: {
    admin: {
      isLogin: false,
      token: getToken(),
    }
  },
  mutations: {
    setLogin(state) {
      state.admin.isLogin = true
    },
    setToken(state, token) {
      state.admin.token = token
      setToken(token)
    },
  },
  actions: {
    handleLogin({
      commit
    }, {
      username,
      password
    }) {
      return new Promise((resolve, reject) => {
        login(username, password).then(res => {
          if (res.data.code === "200") {
            // dispatch('handleAllInfo')
            const data = res.data.data
            if (data === '教师') {
              commit('setLogin')
              commit('setToken', data.token)
              resolve("success")
            } else {
              // iView.Message.error('')
              resolve("账号权限不足")
            }
          } else {
            resolve(res.data.data)
          }
        }).catch(err => {
          reject(err)
        })
      })
    },
  },
  modules: {}
})